package lion.project.blackboard_for_education;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class inputViewActivity extends Activity implements View.OnClickListener {
	private final static String TAG ="blackboard_for_education";
	private Button enterButton;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v(TAG,"inputViewActivity:onCreate");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.inputform);
		this.enterButton = (Button) findViewById(R.id.enterbutton);
		enterButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		
		Log.v(TAG,"onClick");
		//現在表示しているViewを画像に変換する
		if (v == this.enterButton) {
			View view = this.enterButton.getRootView();
			view.setDrawingCacheEnabled(true);
			Bitmap bitmap = view.getDrawingCache();
			if (bitmap == null)
				return;

			try {
				byte[] w = bmp2data(bitmap, Bitmap.CompressFormat.JPEG, 80);
				writeDataFile("inputText.jpg", w);
			} catch (Exception e) {

			}
		}

		//todo. 文字列をなんらかの方法でメインに通知する
	}

	private byte[] bmp2data(Bitmap src, Bitmap.CompressFormat format, int qualioty) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		src.compress(format, qualioty, os);
		return os.toByteArray();
	}

	private void writeDataFile(String name, byte[] w) throws Exception {
		OutputStream out = null;
		try {
			out = openFileOutput(name, Context.MODE_WORLD_READABLE);
			out.write(w, 0, w.length);
			out.close();
		} catch (Exception e) {
			try {
				if (out != null)
					out.close();
			} catch (Exception e2) {
			}
			throw e;
		}
	}
}
